<DBreadcrumbsItem
  @path="/admin/plugins/{{this.adminPluginNavManager.currentPlugin.name}}/leaderboards"
  @label={{i18n "gamification.leaderboard.title"}}
/>

<div class="discourse-gamification__leaderboards admin-detail">
  <DPageSubheader @titleLabel={{i18n "gamification.leaderboard.title"}}>
    <:actions as |actions|>
      {{#if this.model.leaderboards}}
        <actions.Primary
          @label="gamification.leaderboard.new"
          @title="gamification.leaderboard.new"
          class="leaderboard-admin__btn-new"
          @action={{action (mut this.creatingNew) true}}
        />

        <actions.Default
          @label="gamification.recalculate"
          @title="gamification.recalculate"
          class="leaderboard-admin__btn-recalculate"
          @action={{action "recalculateScores"}}
        />
      {{/if}}
    </:actions>
  </DPageSubheader>

  {{#if this.creatingNew}}
    <AdminCreateLeaderboard @onCancel={{action this.resetNewLeaderboard}} />
  {{/if}}

  <div class="leaderboards">
    {{#if this.model.leaderboards}}
      <table>
        <thead>
          <th>{{i18n "gamification.admin.name"}}</th>
          <th>{{i18n "gamification.admin.period"}}</th>
          <th></th>
        </thead>

        <tbody>
          {{#each this.sortedLeaderboards as |leaderboard|}}
            <tr id={{concat "leaderboard-admin__row-" leaderboard.id}}>
              <td>
                <LinkTo
                  @route="gamificationLeaderboard.byName"
                  @model={{leaderboard.id}}
                >
                  {{leaderboard.name}}
                </LinkTo>
              </td>
              <td>
                {{#if leaderboard.fromDate}}
                  {{format-date (this.parseDate leaderboard.fromDate)}}
                  -
                  {{format-date (this.parseDate leaderboard.toDate)}}
                {{else}}
                  {{leaderboard.defaultPeriod}}
                {{/if}}
              </td>
              <td style="width: 120px">
                <div class="leaderboard-admin__listitem-action">
                  <LinkTo
                    @route="adminPlugins.show.discourse-gamification-leaderboards.show"
                    @model={{leaderboard}}
                    class="btn leaderboard-admin__edit btn-text btn-small"
                  >{{i18n "gamification.edit"}} </LinkTo>

                  <DButton
                    class="btn-small leaderboard-admin__delete btn-danger"
                    @icon="trash-can"
                    @title="gamification.delete"
                    @action={{action "destroyLeaderboard" leaderboard}}
                  />
                </div>
              </td>
            </tr>
          {{/each}}
        </tbody>
      </table>
    {{else}}
      {{#unless this.creatingNew}}
        <div class="admin-plugin-config-area__empty-list">
          {{i18n "gamification.leaderboard.none"}}
          <DButton
            @label="gamification.leaderboard.cta"
            class="btn-default btn-small leaderboard-admin__cta-new"
            @action={{action (mut this.creatingNew) true}}
          />
        </div>
      {{/unless}}
    {{/if}}
  </div>
</div>